package com.kingdee.jdy.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.jdy.util.HttpUtil;
import java.util.HashMap;
import java.util.Random;

/**
 * 科目
 */
public class AccountController {

    public static long saveAccount(HashMap<String, Object> urlParam, HashMap<String, Object> headerParam)
        throws Exception {

        long account_id = 0;
        JSONObject jsonStock = AccountController.prepareAccountParam();
        JSONObject resStock = HttpUtil
            .doPost("http://api.kingdee.com/jdy/gl/account_save", urlParam, headerParam, jsonStock.toJSONString());

        if (resStock != null) {
            JSONArray datas = resStock.getJSONArray("data");
            if (datas != null && datas.size() > 0) {
                JSONObject data = datas.getJSONObject(0);

                String success = data.getString("success");
                if (success.equalsIgnoreCase("true")) {
                    account_id = data.getJSONArray("successPkIds").getLongValue(0);
                    System.out.println("同步科目成功！account_id=" + account_id);
                } else {
                    System.out.println("同步科目失败！");
                }
            } else {
                System.out.println("同步科目失败！");
            }
        } else {
            System.out.println("同步科目失败！");
        }
        return account_id;
    }


    public static long getOneAccount(HashMap<String, Object> urlParam, HashMap<String, Object> headerParam)
        throws Exception {

        long account_id = 0;
        JSONObject resCustomer = HttpUtil
            .doPost("http://api.kingdee.com/jdy/gl/account_list", urlParam, headerParam, null);

        if (resCustomer != null) {
            JSONObject data = resCustomer.getJSONObject("data");
            if (data != null) {
                int curpagesize = data.getIntValue("curpagesize");
                if (curpagesize > 0) {
                    account_id = data.getJSONArray("rows").getJSONObject(0).getLongValue("id");
                }
            } else {
                System.out.println("查询科目失败！");
            }
        } else {
            System.out.println("查询科目失败！");
        }
        return account_id;
    }



    public static JSONObject prepareAccountParam() {
        JSONObject item = new JSONObject();
        item.put("id", null);
        item.put("name", "demo测试科目" + new Random().nextInt());
        item.put("number", (1800 + new Random().nextInt(100) + "").substring(0,4));
        item.put("helpcode", null);
        item.put("level", 1);
        item.put("accounttype_id", 101);//流动资产类别
        item.put("isassist", true);

        JSONArray checkitementry = new JSONArray();
        JSONObject entry = new JSONObject();
        entry.put("id", null);
        entry.put("bd_auxinfo_type_id", null);
        entry.put("type", "bd_customer");//客户核算
        checkitementry.add(entry);

        item.put("checkitementry", checkitementry);

        JSONArray items = new JSONArray();
        items.add(item);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("items", items);

        return jsonObject;
    }



}
